import config,json,requests
import time
import pyotp
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
}

def gettotpkey(key)->str:
    """
    获取totp的6位动态口令
    """
    try:
        if config.gcon("setting","totp") != 1:
            totp = pyotp.TOTP(key)
            return totp.now()
    except Exception as e:
        totp = pyotp.TOTP(key)#如果没有该配置项就会进来
        return totp.now()

    try:
        res = requests.post("http://www.2fafb.com/api/jiekou.php",data={"tok":key},headers=header)
    except Exception as e:
        print(e)
        return "000000"
    if res.status_code != 200:
        return "000000"
    try:
        data = json.loads(res.text)
        print(data)
        return data['data']
    except Exception as e:
        print(e)
        return "000000"
if __name__== '__main__':
    key = config.gcon("setting","key")
    name = config.gcon("setting","name")
    while True:
        totp = gettotpkey(key)
        print(totp)
        time.sleep(3)